import { requestHttp } from "@/utils/request";
import { ReqPage, ResultData } from "@/api/types";
import { QuestionBankItem } from "@/api/question/bankItem";

//获取表单题库详细信息
export const pageQuestionBank = (params: PageQuestionParam): Promise<ResultData<PageQuestionParam>> =>
  requestHttp.get("/form/question-bank/page", params);

//获取表单题库详细信息
export const listQuestionBank = (bankType: number | null, isPublic?: boolean): Promise<ResultData<QuestionBank[]>> =>
  requestHttp.get("/form/question-bank/list", { bankType: bankType, isPublic: isPublic });
//获取表单题库详细信息
export const getQuestionBank = (id: number): Promise<ResultData<QuestionBank>> => requestHttp.get("/form/question-bank/" + id);
//新增表单题库
export const addQuestionBank = (data: QuestionBank): Promise<ResultData<QuestionBank>> =>
  requestHttp.post("/form/question-bank/add", data);
//修改表单题库
export const updateQuestionBank = (data: QuestionBank): Promise<ResultData<QuestionBank>> =>
  requestHttp.post("/form/question-bank/update", data);
//删除表单题库
export const deleteQuestionBank = (id: number | number[]): Promise<ResultData<QuestionBank>> =>
  requestHttp.post("/form/question-bank/delete/" + id);

//查询题库信息
export const getQuestionBankInfo = (): Promise<ResultData<FormQuestionBank>> => requestHttp.get("/form/question-bank/info");

//查询题库题目
export const searchQuestionBankItems = (name: string): Promise<ResultData<SearchQuestionBankItems>> =>
  requestHttp.get(`/form/question-bank/search`, { name });

//查询题库题目
export const getQuestionBankItems = (bankId: number): Promise<ResultData<QuestionBankItem[]>> =>
  requestHttp.get(`/form/question-bank/${bankId}/questions`);

/**
 * 根据id删除
 * @param id id
 */
export const deleteQuestionBankById = (id: number): Promise<ResultData> =>
  requestHttp.post("/form/question-bank/my/delete", { id });

export interface PageQuestionParam extends ReqPage {
  bankType: number | null;
}

export interface SearchQuestionBankItems {
  personalQuestionBankItems: QuestionBankItem[];
  publicQuestionBankItems: QuestionBankItem[];
}

export interface QuestionBankInfo {
  personalQuestionBank: QuestionBank[];

  publicQuestionBank: QuestionBank[];
}

/**
 * FormQuestionBankVO
 */
export interface FormQuestionBank {
  /**
   * 个人题库
   */
  personalQuestionBanks?: QuestionBank[] | null;
  /**
   * 公共题库
   */
  publicQuestionBanks?: QuestionBank[] | null;
}

export interface QuestionBank {
  createTime?: string;
  description: string;
  id?: number | null;
  isPublic?: boolean;
  name: string;
  updateTime?: string;
  bankType: number;
  itemCount?: number;
}

export interface PageQuestionParam extends ReqPage {
  total: number;
  name: string;
  records?: QuestionBank[];
}
